'use strict'
let nodeServerConfig  = require('../config/serverConfig').nodeServerConfig
let ws = require('ws').Server
let nodeMgr = require('./nodeMgr')

class RootServer {
  constructor() {
    this.server = null
    this.curId = 1
  }

  //创建WS服务器
  createServer() {
    let self = this
    this.server = new ws({
      host: nodeServerConfig.ip,
      port: nodeServerConfig.port
    })
    console.log(`网关主服务器启动 ${nodeServerConfig.ip}:${nodeServerConfig.port}`)
    this.server.on('connection', function(socket) {
      //创建会话
      console.log('新子节点连接')
      nodeMgr.createNode(self.curId, socket)
      self.curId += 1
    })
  }

}

let rootServer = new RootServer()
rootServer.createServer()
module.exports = rootServer
